﻿using CarAppClient.Consts;
using CarAppClient.Controls.MessageUsercontrols;
using CarAppClient.Extension;
using CarAppClient.Helpers;
using CarAppClient.ViewModels;
using CarAppClient.Views.CarType.Views;
using CarAppDto.CarType.VehicleSeriesDtos;
using CarAppDto.CarType.CarDtos;
using D.UtilCore.Dtos;
using CarAppDto.CarType.AreaDtos;
using CarAppClient.Views.CarType.Views.VehicleSeries;
using System.Collections.ObjectModel;
using CarAppClient.Views.Country;
using CarAppDto.CarType.CountryDtos;
using CarAppDto.CarType.BrandDtos;
using CarAppClient.Views.CarType.Views.Brand;
using CarAppDto.Common;

namespace CarAppClient.Views.CarType.ViewModels.CarType
{
    public class VehicleSeriesModifyViewModel : BaseModifyViewModel<Page_VehicleSeries_Modify>
    {      
        #region 加载页面数据

        public VehicleSeriesModifyViewModel(Page_VehicleSeries_Modify owner, string? id, ViewStatus? viewStatus) : base(owner, id, viewStatus)
        {
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="repCarType"></param>
        protected async override void LoadData()
        {
            if (!string.IsNullOrEmpty(Id))
            {
                // 请求后端接口 
                var res = await HttpHelper<RepVehicleSeries>.GetAsync($"{ApiUrl.GetVehicleSeries}/{Id}");

                //  判断结果
                if (res.Code == StatusCodeEnum.Waring)
                {
                    MsgUtil.Error(res.ErrorMsg);
                }
                else if (res.Code == StatusCodeEnum.Error)
                {
                    MsgUtil.Error("服务器异常");
                }
                else
                {
                    // 绑定数据源到UI 
                    if (res.Data != null)
                    {
                      
                        Name = res.Data.Name;
                        Code = res.Data.Code;                      
                        EnglishName = res.Data.EnglishName;
                        CarSeriesModel=res.Data.CarSeriesModel;
                        ModelYearCode=res.Data.ModelYearCode;
                        CatalogNo=res.Data.CatalogNo;
                        VehicleSeriesBrevityCoding=res.Data.VehicleSeriesBrevityCoding;
                        VehicleSeriesVersionEnglishName=res.Data.VehicleSeriesVersionEnglishName;
                        VehicleSeriesVersionName=res.Data.VehicleSeriesVersionName;
                        MarketingTime = res.Data.MarketingTime;
                        DownTime=res.Data.DownTime;
                        SaleDownTime=res.Data.SaleDownTime;
                        SelectedSaleStatus = ConvertSaleStatusText(res.Data.SaleStatus);

                        var resArea = await HttpHelper<RepArea>.GetAsync($"{ApiUrl.GetArea}/{res.Data.SaleAreaId}");
                        repArea = resArea.Data;
                        SaleArea = repArea?.Name;

                        var resCountry = await HttpHelper<RepCountry>.GetAsync($"{ApiUrl.GetCountry}/{res.Data.ManufacturingCountryId}");
                        repCountry = resCountry.Data;
                        ManufacturingCountry = repCountry?.Name;

                        var resBrand = await HttpHelper<RepBrand>.GetAsync($"{ApiUrl.GetBrand}/{res.Data.BrandId}");
                        repBrand = resBrand.Data;
                        Brand = repBrand?.Name;
                    }
                }
            }
        }
        #endregion

        #region 新增、修改 参数

        private string? name;
        /// <summary>
        /// 名称
        /// </summary>
        public string? Name
        {
            get { return name; }
            set { name = value; RaisePropertyChanged(); }
        }

        private string? code;
        /// <summary>
        /// 编号
        /// </summary>
        public string? Code
        {
            get { return code; }
            set { code = value; RaisePropertyChanged(); }
        }

        private string? englishName;
        /// <summary>
        /// 描述
        /// </summary>
        public string? EnglishName
        {
            get { return englishName; }
            set { englishName = value; RaisePropertyChanged(); }
        }

        RepArea repArea = null;
        private string? area;
        /// <summary>
        /// 销售区域
        /// </summary>
        public string? SaleArea
        {
            get { return area; }
            set { area = value; RaisePropertyChanged(); }
        }


        RepCountry repCountry = null;
        private string? country;
        /// <summary>
        /// 制造国家
        /// </summary>
        public string? ManufacturingCountry
        {
            get { return country; }
            set { country = value; RaisePropertyChanged(); }
        }

        RepBrand repBrand = null;
        private string? brand;
        /// <summary>
        /// 品牌
        /// </summary>
        public string? Brand
        {
            get { return brand; }
            set { brand = value; RaisePropertyChanged(); }
        }

        public ObservableCollection<string> SaleStatusList { get; set; }=new ObservableCollection<string>() { "正销","停销","停产"};

        private string? selectedSaleStatus= "正销";
        /// <summary>
        /// 选择的销售状态
        /// </summary>
        public string? SelectedSaleStatus
        {
            get { return selectedSaleStatus; }
            set { selectedSaleStatus = value; RaisePropertyChanged(); }
        }

        private string? carSeriesModel;
        /// <summary>
        /// 车系型号
        /// </summary>
        public string? CarSeriesModel
        {
            get { return carSeriesModel; }
            set { carSeriesModel = value; RaisePropertyChanged(); }
        }

        private string? modelYearCode;
        /// <summary>
        /// 年代代码
        /// </summary>
        public string? ModelYearCode
        {
            get { return modelYearCode; }
            set { modelYearCode = value; RaisePropertyChanged(); }
        }

        private string? catalogNo;
        /// <summary>
        /// 目录号
        /// </summary>
        public string? CatalogNo
        {
            get { return catalogNo; }
            set { catalogNo = value; RaisePropertyChanged(); }
        }

        private string? vehicleSeriesBrevityCoding;
        /// <summary>
        /// 车系简码
        /// </summary>
        public string? VehicleSeriesBrevityCoding
        {
            get { return vehicleSeriesBrevityCoding; }
            set { vehicleSeriesBrevityCoding = value; RaisePropertyChanged(); }
        }

        private string? vehicleSeriesVersionEnglishName;
        /// <summary>
        /// 车系版本英文名称
        /// </summary>
        public string? VehicleSeriesVersionEnglishName
        {
            get { return vehicleSeriesVersionEnglishName; }
            set { vehicleSeriesVersionEnglishName = value; RaisePropertyChanged(); }
        }

        private string? vehicleSeriesVersionName;
        /// <summary>
        /// 车系版本中文名称
        /// </summary>
        public string? VehicleSeriesVersionName
        {
            get { return vehicleSeriesVersionName; }
            set { vehicleSeriesVersionName = value; RaisePropertyChanged(); }
        }

        private DateTime? marketingTime;
        /// <summary>
        ///上市时间
        /// </summary>
        public DateTime? MarketingTime
        {
            get { return marketingTime; }
            set { marketingTime = value; RaisePropertyChanged(); }
        }

        private DateTime? downTime;
        /// <summary>
        ///停产时间
        /// </summary>
        public DateTime? DownTime
        {
            get { return downTime; }
            set { downTime = value; RaisePropertyChanged(); }
        }

        private DateTime? saleDownTime;
        /// <summary>
        ///停产时间
        /// </summary>
        public DateTime? SaleDownTime
        {
            get { return saleDownTime; }
            set { saleDownTime = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 保存事件
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="vm"></param>
        protected override async void Submit(object? obj)
        {
            // 基础数据判断
            if (string.IsNullOrEmpty(Name))
            {
                MsgUtil.Warning("区域名称不可为空"); return;
            }

            // 基础数据判断
            if (string.IsNullOrEmpty(Id))
            {
                // 请求后端接口 
                var res = await HttpHelper<int>.PostAsync(ApiUrl.AddVehicleSeries, new ReqAddVehicleSeries()
                {
                    Name = Name,
                    Code = Code,
                    EnglishName = EnglishName,
                    BrandId = repBrand?.Id,
                    CarSeriesModel = CarSeriesModel,
                    SaleAreaId = repArea?.Id,
                    ManufacturingCountryId = repCountry?.Id,
                    ModelYearCode = ModelYearCode,
                    CatalogNo = CatalogNo,
                    VehicleSeriesBrevityCoding = VehicleSeriesBrevityCoding,
                    VehicleSeriesVersionEnglishName = VehicleSeriesVersionEnglishName,
                    VehicleSeriesVersionName = VehicleSeriesVersionName,
                    MarketingTime = MarketingTime,
                    DownTime = DownTime,
                    SaleDownTime = SaleDownTime,
                    SaleStatus = ConvertSaleStatus(SelectedSaleStatus)
                }); 

                //  判断结果
                MsgUtil.ShowMsg(res.Code, "新增成功", res.ErrorMsg);
            }
            else
            {
                // 请求后端接口 
                var res = await HttpHelper<int>.PutAsync($"{ApiUrl.UpdateVehicleSeries}", new ReqUpdateVehicleSeries()
                {
                    Id = Id,
                    Name = Name,
                    Code = Code,
                    EnglishName = EnglishName,
                    BrandId = repBrand?.Id,
                    CarSeriesModel = CarSeriesModel,
                    SaleAreaId = repArea?.Id,
                    ManufacturingCountryId = repCountry?.Id,
                    ModelYearCode = ModelYearCode,
                    CatalogNo = CatalogNo,
                    VehicleSeriesBrevityCoding = VehicleSeriesBrevityCoding,
                    VehicleSeriesVersionEnglishName = VehicleSeriesVersionEnglishName,
                    VehicleSeriesVersionName = VehicleSeriesVersionName,
                    MarketingTime = MarketingTime,
                    DownTime = DownTime,
                    SaleDownTime = SaleDownTime,
                    SaleStatus = ConvertSaleStatus(SelectedSaleStatus)
                });
                // 判断结果
                MsgUtil.ShowMsg(res.Code, "修改成功", res.ErrorMsg);
            }
            this.FromClose();
        }
        #endregion


        protected override async void Query(string? type)
        {
            if (type == "Area")
            {
                Page_Area_Query area_Query = new Page_Area_Query();
                ((AreaQueryViewModel)area_Query.DataContext).InformationRequested += Area_InformationRequested;
                area_Query.ShowDialog();
            }
            else if (type == "Country")
            {
                Page_Country_Query country_Query = new Page_Country_Query();
                ((CountryQueryViewModel)country_Query.DataContext).InformationRequested += Country_InformationRequested;
                country_Query.ShowDialog();
            }
            else if (type == "Brand")
            {
                Page_Brand_Query brand_Query = new Page_Brand_Query();
                ((BrandQueryViewModel)brand_Query.DataContext).InformationRequested += Brand_InformationRequested;
                brand_Query.ShowDialog();
            }

        }

        private void Area_InformationRequested(object sender, InformationEventArgs<RepArea> e)
        {
            // 处理从ChildViewModel传递过来的信息
            repArea = e.Information;
            if (repArea?.Name != SaleArea)
            {
                repCountry = null;
                ManufacturingCountry = "";
            }
            SaleArea = repArea?.Name;

        }

        private void Country_InformationRequested(object sender, InformationEventArgs<RepCountry> e)
        {
            // 处理从ChildViewModel传递过来的信息
            repCountry = e.Information;
            ManufacturingCountry = repCountry?.Name;
        }

        private void Brand_InformationRequested(object sender, InformationEventArgs<RepBrand> e)
        {
            // 处理从ChildViewModel传递过来的信息
            repBrand = e.Information;
            Brand = repBrand?.Name;
        }

        int ConvertSaleStatus(string status)
        { 
          switch (status)
            {
                case "正销": return 0;break;
                case "停销": return 1; break;
                case "停产": return 2; break;
            }
            return -1;
        }

        string ConvertSaleStatusText(int? status)
        {
            switch (status)
            {
                case 0: return "正销"; break;
                case 1: return "停销"; break;
                case 2: return "停产"; break;
            }
            return "";
        }
    }
}
